#include "head.h"

// 单链表的创建
U_list *S_List_init()
{
    // 创建头节点
    U_list *list = (U_list *)malloc(sizeof(U_list));
    // 判断是否创建成功
    if (list != NULL)
    {
        // 如果成功则初始化链表中的头节点为 NULL
        list->next = NULL;
    }
    else
    {
        printf("创建失败\n");
        return NULL;
    }
    // 返回链表结构体
    return list;
}

// 单链表的插入
void S_List_insert(U_list *list, U_data data, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *new_node = (U_node *)malloc(sizeof(U_node));
    if (new_node == NULL)
    {
        printf("new_node创建失败\n");
        return;
    }
    new_node->next = NULL;
    new_node->data = data;

    if (pos == 1)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }

    U_node *ptr = list->next;

    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }

    if (ptr == NULL)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;

    return;
}

// 单链表的删除
void S_List_delete(U_list *list, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = NULL;
    if (pos == 1)
    {
       // list->next = ptr_1->next;
       // free(ptr_1);
        U_node *temp = list->next; // 保存第一个节点
        list->next = temp->next;    // 将头节点的next指向第一个节点的next
        free(temp);                 // 释放第一个节点的内存
        return;
    }

    for (size_t i = 0; i < pos - 1 && ptr_1->next != NULL; i++)
    {
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (ptr_1 == NULL)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2->next = ptr_1->next;

    free(ptr_1);
    return;
}
/*
// 单链表的清空
int clear_link_list(U_list* list)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }

    U_node * ptr_node_1  = list->next;
    U_node * ptr_node_2  = ptr_node_1->next;

    // 销毁结点
    for (size_t i = 0; NULL != ptr_node_1 ; i++)
    {
        free(ptr_node_1);
        // 移动指针
        ptr_node_1 = ptr_node_2;

        if (NULL != ptr_node_2)
        {
            ptr_node_2 = ptr_node_2->next;
        }
    }

    return 0;
}
*/
int S_List_name_delete(U_list *list,char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return EOF;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return EOF;
    }

    U_node *ptr = list->next;
    int i=0;
    while (ptr != NULL)
    {     i++;
         if(0==strcmp(ptr->data.name, name) )
        {
            printf("i:%d",i);
            S_List_delete(list, i);
            
            return i;
        }
        printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;

    }
    return 0;
}

U_data S_List_find_name_data(U_list *list, char *name,int grade)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return ;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            ptr->data.grade=grade;
            return ptr->data;
        }
        ptr = ptr->next;
    }

    return ;
}

// 单链表的查找
U_node *S_List_find_name(U_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            return ptr;
        }
        ptr = ptr->next;
    }

    return NULL;
}
//通过航班序号得出要改哪个下标的票
//@id 车次
//@i  data.ticket[i] 的下标
int S_List_find_id(U_list *list, int id,int i)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return EOF;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (ptr->data.ticket[i].id==id)
        {
            return i;
        }
        ptr = ptr->next;
    }

    return EOF;
}





int S_list_up_data_passwd(U_list* list, U_node * node,char *passsword)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }

    strcpy(node->data.passwd,passsword);

    return 0;
}



int S_list_add_recharge(U_list* list, U_node * node,int recharge)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }

    node->data.value+=recharge;
    return 0;
}

int S_list_add_id(U_list* list, U_node * node,int id,int num,int flight_fare)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }

    node->data.ticket[num].id=id;
    node->data.value=node->data.value-flight_fare;
    return 0;
}

int S_list_up_data(U_list* list, U_node * node)
{
   ERRLOG("output");
    //node->data;
  
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }

    // 打印原始数据

     int flag=0;
    U_data temp_data=node->data;
 // 开始修改
    const wchar_t *title_user_revise= L"用户信息修改菜单";
    const wchar_t *option_user_revise[] ={L"当前用户信息",L"用户名",L"密码",L"身份码",L"余额",L"持有机票",L"确认修改",L"取消修改"};
    system("clear");
    while (flag < 7)
    {   
       // ERRLOG("output1");
        flag=display_menu(title_user_revise, option_user_revise, 8);
       // ERRLOG("output2");

        switch (flag)
        {
        case 1:
            system("clear");
            printf("查看当前信息 \r\n");
            printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               temp_data.name, temp_data.passwd, temp_data.grade, temp_data.value);
            printf("\n持有的票数量与航班班次:\n");
            printf("\n票数\t航班班次\t\n");
            
            for (size_t i = 0; i < 10&&0!=node->data.ticket[i].id; i++)
            {  
                printf("%zu\t%d\t\n",i+1,node->data.ticket[i].id); 
            }
            printf("____________回车以返回___________");
            getchar();
            getchar();
            
            break;
        case 2:
            printf("用户名:");
            scanf("%s", temp_data.name);
            break;
        case 3:
            printf("密码:");
            scanf("%s", temp_data.passwd);
            break;
        case 4:
            printf("身份码:");
            scanf("%d",&temp_data.grade);
            break;
        case 5:
            printf("余额:");
            scanf("%d", &temp_data.value);
            break;
        case 6:
            printf("持有的票:");
            for (size_t i = 0; i < 10; i++)
            {  
                printf("%zu:%d\t",i,node->data.ticket[i].id); 
            }
            for (size_t i = 0; i < 10; i++)
            {  
                printf("\n请输入第%zu张票的航班班次:%d\n",i,node->data.ticket[i].id);
                scanf("%d", &temp_data.ticket[i].id); 
            }
            break;
         case 7:
            node->data=temp_data;
            printf("保存退出");
            break;
        default:
            printf("取消修改");
            break;
        }
    }
    
    return 0;
}




// 单链表的遍历
void S_List_print(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }
}

// 单链表的清空
void S_List_clear(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的合并
U_list *S_List_merge(U_list *list1, U_list *list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("链表不存在\n");
        return list1;
    }

    // 1、求链表list1 的链表长度
    U_node *ptr_1 = list1->next;
    int len = 0;

    while (ptr_1 != NULL)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    // 插入到链表中
    ptr_1 = list1->next;
    U_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        S_List_insert(list1, ptr_2->data, len);
        ptr_2 = ptr_2->next;
    }

    return list1;
}

// 单链表的销毁
void S_List_free(U_list **list)
{
    if (list == NULL || *list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *ptr_1 = (*list)->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }

    free(*list);
    *list = NULL;
}

// 向文件写入用户信息
void S_List_File_print(U_list *list, const char *file_name)
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return ;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        printf("链表无数据\n");
        FILE *fp2 = fopen(file_name, "w+");
        fclose(fp2);
        return ;
    }

    FILE *fp = fopen(file_name, "w+");
    if (fp == NULL)
    {
        printf("文件创建失败\n");
        return;
    }
    fseek(fp, 0, SEEK_SET);

    U_node *ptr = list->next;

    while (ptr != NULL)
    {
        fprintf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\t",
                ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
                for (size_t i = 0; i < 10; i++)
                {
                    if(9==i)
                    {
                    fprintf(fp,"%zu:%d\n",i,ptr->data.ticket[i].id);
                    break;
                    }
                    fprintf(fp,"%zu:%d\t",i,ptr->data.ticket[i].id);
                }
                
        ptr = ptr->next;
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}

// 从文件读取用户信息
void S_List_File_scanf(U_list *list, const char *file_name)
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    // 移动光标
    fseek(fp, 0, SEEK_SET);
    // 清空链表
    S_List_clear(list);

    U_data data;
    char ch = 0;
    int pos = 0;

    // 检测文件行数
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }

    // 移动光标
    fseek(fp, 0, SEEK_SET);

    for (size_t i = 0; i < pos; i++)
    {
        // "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n"
        fscanf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\t",
               data.name, data.passwd, &data.grade, &data.value);

                 for (size_t i = 0; i < 10; i++)
                {
                    if(9==i)
                    {
                    fscanf(fp,"%zu:%d\n",&i,&data.ticket[i].id);
                    break;
                    }
                    fscanf(fp,"%zu:%d\t",&i,&data.ticket[i].id);
                }
        S_List_insert(list, data, i + 1);
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}
